<template>
  <div
    style="
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
    "
  >
    <div>
      <el-form>
        <el-form-item label="纬度">
          <el-input v-model:value="loc.lat"></el-input>
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model:value="loc.lng"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-map-loc">
      <div id="container"></div>
    </div>
  </div>
  <el-row class="login-container">
    <el-col :lg="14" :md="12" class="left"></el-col>
    <!-- 登录表单 -->
    <el-col :lg="10" :md="12" class="right">
      <div>欢迎回来</div>
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>
      <el-form ref="formRef" :rules="rules" :model="form" class="w-[250px]">
        <el-form-item prop="userId">
          <el-input v-model="form.userId" placeholder="请输入账号">
            <template #prefix>
              <el-icon class="el-input__icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            show-password
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
            class="w-[250px]"
            round
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
let loc = ref({
  lat: 39.984104,
  lng: 116.307503,
  address: "",
});

watch(
  () => loc.value.lat,
  (val, o) => {
    console.log("val is ", val);
    flushLatLon();
  }
);

watch(
  () => loc.value.lng,
  (val, o) => {
    console.log("val is ", val);
    flushLatLon();
  }
);

// 刷新地图
function flushLatLon() {
  var element = document.getElementById("container");
  while (element.firstChild) {
    element.removeChild(element.firstChild);
  }

  var center = new TMap.LatLng(loc.value.lat, loc.value.lng); //设置中心点坐标
  //初始化重新定位
  var map = new TMap.Map("container", {
    center: center,
  });
}
// 组件挂载后，渲染对应组件
onMounted(() => {
  var center = new TMap.LatLng(loc.value.lat, loc.value.lng); //设置中心点坐标
  //初始化地图
  var map = new TMap.Map("container", {
    center: center,
  });

  //绑定点击事件
  map.on("click", function (evt) {
    console.log("event is ", evt);
    var lat = evt.latLng.getLat().toFixed(6);
    var lng = evt.latLng.getLng().toFixed(6);
    loc.value.lat = lat;
    loc.value.lng = lng;
  });
});
</script>

<style scoped lang="scss">
.common-map-loc {
  position: relative;
  #container {
    width: 800px;
    height: 500px;
  }
  #info {
    position: absolute;
    left: 20px;
    top: 20px;
    font-size: 14px;
    background: #fff;
    width: 40%;
    padding: 10px;
    border-radius: 3px;
  }
}
.search-div {
  display: flex;
  justify-content: center;
}
</style>
